package xin.nick.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.nick.constant.SystemConstants;
import xin.nick.domain.dto.LoginDTO;
import xin.nick.domain.vo.CurrentUserInfoVO;
import xin.nick.domain.vo.LoginUserVO;
import xin.nick.service.LoginService;
import xin.nick.service.SystemUserService;

import javax.validation.Valid;


/**
 * @author Nick
 * @since 2022/7/26/026
 */
@Tag(name = "登录模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class LoginController {

    private final LoginService loginService;
    private final SystemUserService systemUserService;

    @Operation(summary = "登录")
    @PostMapping("/login")
    public LoginUserVO login(@RequestBody @Valid LoginDTO loginDTO) {
        return loginService.login(loginDTO);
    }

    @Operation(summary = "退出登录",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @DeleteMapping("/logout")
    public void logout() {
        loginService.logout();
    }

    @Operation(summary = "获取用户信息",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getUserInfo")
    public CurrentUserInfoVO getUserInfo() {
        return systemUserService.getCurrentUserInfo();
    }

}
